#ifndef BOOKCARINTERFACE_H
#define BOOKCARINTERFACE_H

#include <QWidget>
#include "globalParameter.h"
#include "interface/IInterface.h"
#include "qtablewidget.h"
#include <QTimer>
#include <vector>
#include "socket/protocol/ClientBook.h"
#include "socket/protocol/ClientChooseCar.h"

using namespace std;

namespace Ui {
    class ChooseCar;
}

namespace interface
{
struct ChooseCarParam
{
    QString carId;
    QString clientId;
};


//======================================================================
// Class:   CChooseCarInterface
//
// Description:
//      This class is responsible for show out the choose-car interface. It must be shown
//      after interface::BOOK_CAR, and reveive the filled structure protocal::ClientBook.
//      In this class, we also have a timer to send request to server for fetching other willing-to-
//      take-passagers taxi drivers' information, and append them to the table widget.
//
//  Notes:
//	None.
//
#define COLUMNCOUNT 5
#define TIMEOUT 5000 // every 5 seconds will emit a signal to invoke timer
class CChooseCarInterface : public IInterface
{
    Q_OBJECT

public:
    explicit CChooseCarInterface(IInterface *parent = 0, protocal::ClientBook *clientBookInfo = 0);
    ~CChooseCarInterface();

private Q_SLOTS:
    // button OK
    void chooseTheCar();
    // button CANCEL
    void cancelChoose();
    // when the communication between server and client is over,
    // this function will be invoked
    void readResult();
    // an error occures when communicating with server
    void socketError();

    // slots for table widget
    // when an item of the table has been clicked, this function will be invoked.
    void SelectTableItem( int row, int column );

    // when the timer is timeout, this slot will be invoked
    void sendRequestByTime();

private:
    // prepare for the background setting
    virtual void UiSetting(Ui::ChooseCar *ui);
    // fill the table
    void FillTable(QTableWidget * pTableView, QMap<QString, QString> * pCarInfoMap);
    // format the table
    void FormatTable(QTableWidget * pTableView);

    static void decreaseDriverNum() {m_sAvailableDriverNum--; }
    static void increaseDriverNum() {m_sAvailableDriverNum++; }
    static int getDriverNum() {return m_sAvailableDriverNum; }
    static void clearDriverNum() {m_sAvailableDriverNum = 0; }

private:
    Ui::ChooseCar *m_pUi;
    protocal::ClientChooseCar *m_pClientChooseCar;
    ChooseCarParam m_param;
    // used to book the taxi
    protocal::ClientBook *m_pClientBookInfo;

    int m_chosenDriver; // it is the row number of the table cell

    // a timer that will send the request every TIMEOUT ms to request for
    // other taxi infomation if there exist
    QTimer *m_pTimer;

    //items for table view
    vector<vector<QTableWidgetItem*> > m_vecItem;

    // a variaty that controls the number of current available drivers
    static int m_sAvailableDriverNum;

    // a map between each table row and their corresponding car_id
    QMap<int, QString> m_mapRowToCarID;

    // enum for tale column order, which must aglin to the extract order in the table
    enum
    {
        LICENCE = 0,
        DRIVER_NAME,
        COMPANY,
        HONEST_DEGREE,
        ARRIVING_TIME
    };
};
}

#endif // BOOKCARINTERFACE_H
